package com.jms.fu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jms.fu.exception.DAOException;
import com.jms.fu.model.bean.App;
import com.jms.fu.model.bo.AppBO;
import com.jms.fu.model.bo.AppDownBO;

public class ViewCartAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		PrintWriter out = null;

		String username = (String) session.getAttribute("username");
		String role = (String) session.getAttribute("role");
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
		if (session.getAttribute("cart") == null) {
			out.print("failed");
			return null;
		}
		AppBO appbo = new AppBO();
		App app = new App();
		ArrayList<Integer> listApp = (ArrayList<Integer>) session
				.getAttribute("cart");
		AppDownBO appdown = new AppDownBO();
		try {
			String applistHTML = "";
			for (int appid : listApp) {
				app = appbo.getAppByID(appid);
				ArrayList<String> avatar = appbo.getNumberOfFile(getServlet()
						.getServletContext().getRealPath("/")
						+ app.getLink()
						+ "/Avatar");
				String avatarLink;
				if (avatar.size() == 0) {
					avatarLink = "references/images/avatar.jpg";
				} else {
					avatarLink = app.getLink() + "/Avatar/" + avatar.get(0);
				}

				String linkDownload = "";
				if (role != null || username != null) {
					if (app.getPrice() == 0
							|| appdown.checkAppOwnByUsername(username,
									app.getAppId())
							|| app.getDevId().equals(username)
							|| role.equals("Administrator")) {
						String appname = appbo.getNumberOfFile(
								getServlet().getServletContext().getRealPath(
										"/")
										+ app.getLink()).get(0);
						linkDownload = app.getLink() + "/" + appname;

						applistHTML += "<li class='list-group-item'> "
								+ " <div class='col-xs-12 col-sm-3'> <img src='"
								+ avatarLink
								+ "'style='width: 50px;height: 50px;'> </div>"
								+ "<a class='item-g' href='getAppDetail.jms?appid="
								+ app.getAppId() + "'>" + app.getAppName()
								+ "</a> </div>" + "<a class='item-g' href='"
								+ linkDownload + "'> Download </a>"
								+ "<a class='item-g' onclick='removeFromCart("
								+ app.getAppId() + ")'> Delete </a>"
								+ " <div class='clearfix'></div></li>";
					}else{
						applistHTML += "<li class='list-group-item'> "
								+ " <div class='col-xs-12 col-sm-3'> <img src='"
								+ avatarLink
								+ "'style='width: 50px;height: 50px;'> </div>"
								+ "<div class='col-xs-12 col-sm-9'> <span class='price'>"
								+ (app.getPrice() > 0 ? (app.getPrice() + "$")
										: "Free") + "</span><br> "
								+ "<a class='item-g' href='getAppDetail.jms?appid="
								+ app.getAppId() + "'>" + app.getAppName()
								+ "</a> "
								+ "<a class='item-g' onclick='removeFromCart("
								+ app.getAppId() + ")'> Delete </a></div>"
								+ " <div class='clearfix'></div></li>";
					}
				} else {

					applistHTML += "<li class='list-group-item'> "
							+ " <div class='col-xs-12 col-sm-3'> <img src='"
							+ avatarLink
							+ "'style='width: 50px;height: 50px;'> </div>"
							+ "<div class='col-xs-12 col-sm-9'> <span class='price'>"
							+ (app.getPrice() > 0 ? (app.getPrice() + "$")
									: "Free") + "</span><br> "
							+ "<a class='item-g' href='getAppDetail.jms?appid="
							+ app.getAppId() + "'>" + app.getAppName()
							+ "</a> "
							+ "<a class='item-g' onclick='removeFromCart("
							+ app.getAppId() + ")'> Delete </a></div>"
							+ " <div class='clearfix'></div></li>";
				}
			}
			out.print(applistHTML);
		} catch (DAOException e) {
			e.printStackTrace();
			return mapping.findForward("failed");
		}
		return null;
	}

}
